﻿using Library.AppWeb;
using MPR.Poker.Model.SYS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MPR.Poker.DALEntity.SYS
{
    public class DAL_TournamentStepTableMember
    {
        public DAL_TournamentStepTableMember()
        {
        }

        #region Insert/Update/Delete

        /// <summary>
        /// Thêm mới thành viên tham gia tổng hợp
        /// </summary>
        /// <param name="objTournamentStepTableMember"></param>
        /// <returns></returns>
        public ResultMessage Insert(SYS_TournamentStepTableMember objTournamentStepTableMember)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                objResultMessage = CheckExist(objTournamentStepTableMember);
                if (objResultMessage.IsError) return objResultMessage;

                objResultMessage = InsertNoCheck(objTournamentStepTableMember);
                if (objResultMessage.IsError) return objResultMessage;
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Insert, "Lỗi thêm thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> Insert");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Thêm thành viên tham gia thành công", string.Empty);
        }

        /// <summary>
        /// Cập nhật thành viên tham gia tổng hợp
        /// </summary>
        /// <param name="objTournamentStepTableMember"></param>
        /// <returns></returns>
        public ResultMessage Update(SYS_TournamentStepTableMember objTournamentStepTableMember)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                objResultMessage = CheckExist(objTournamentStepTableMember);
                if (objResultMessage.IsError) return objResultMessage;

                objResultMessage = UpdateNoCheck( objTournamentStepTableMember);
                if (objResultMessage.IsError) return objResultMessage;
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Insert, "Lỗi cập nhật thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> Update");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Cập nhật thành viên tham gia thành công", string.Empty);
        }

        /// <summary>
        /// Xóa thành viên tham gia
        /// </summary>
        /// <param name="strTournamentStepTableMemberID"></param>
        /// <returns></returns>
        public ResultMessage Delete(SYS_TournamentStepTableMember objTournamentStepTableMember)
        {
            ResultMessage objResultMessage = new ResultMessage();
            objResultMessage = CheckRelationship(objTournamentStepTableMember);
            if (objResultMessage.IsError) return objResultMessage;

            objResultMessage = DeleteNoCheck(objTournamentStepTableMember);

            return objResultMessage;
        }

        /// <summary>
        /// Xóa thành viên tham gia
        /// </summary>
        /// <param name="intTournamentStepTableMemberIDSys">Mã thành viên tham gia</param>
        /// <param name="intDeletedTournamentStepTableMember">Mã thành viên tham gia</param>
        /// <returns></returns>
        public ResultMessage Delete(int intTournamentStepTableMemberIDSys, int intDeletedTournamentStepTableMember)
        {
            ResultMessage objResultMessage = new ResultMessage();
            SYS_TournamentStepTableMember objTournamentStepTableMember = new SYS_TournamentStepTableMember();
            objTournamentStepTableMember.IDSys = intTournamentStepTableMemberIDSys;

            objResultMessage = CheckRelationship(objTournamentStepTableMember);
            if (objResultMessage.IsError) return objResultMessage;

            objResultMessage = DeleteNoCheck(objTournamentStepTableMember);

            return objResultMessage;
        }

        /// <summary>
        /// Xóa thành viên tham gia
        /// </summary>
        /// <param name="lstTournamentStepTableMemberIDSys">List đối tượng thành viên tham gia</param>
        /// <param name="intDeletedTournamentStepTableMember">Mã thành viên tham gia</param>
        /// <returns></returns>
        public ResultMessage Delete(List<int> lstTournamentStepTableMemberIDSys, int intDeletedTournamentStepTableMember)
        {
            ResultMessage objResultMessage = new ResultMessage();

            foreach (int IDSys in lstTournamentStepTableMemberIDSys)
            {
                SYS_TournamentStepTableMember objTournamentStepTableMember = new SYS_TournamentStepTableMember();
                objTournamentStepTableMember.IDSys = IDSys;

                objResultMessage = CheckRelationship(objTournamentStepTableMember);
                if (objResultMessage.IsError) return objResultMessage;

                objResultMessage = DeleteNoCheck(objTournamentStepTableMember);
                if (objResultMessage.IsError) return objResultMessage;
            }

            return objResultMessage;
        }

        /// <summary>
        /// Kiềm tra tồn tại thành viên tham gia
        /// </summary>
        /// <param name="strID"></param>
        /// <param name="strTournamentStepTableMemberID"></param>
        /// <returns></returns>
        public ResultMessage CheckExist(SYS_TournamentStepTableMember objTournamentStepTableMember)
        {
            ResultMessage objResultMessage = new ResultMessage();
            SYS_TournamentStepTableMember TournamentStepTableMember = new SYS_TournamentStepTableMember();

            try
            {
                
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.CheckData, "Lỗi kiểm tra tồn tại thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> CheckExist");
                return objResultMessage;
            }
            return objResultMessage;
        }

        /// <summary>
        /// Kiểm tra dữ liệu liên quan
        /// </summary>
        /// <param name="strTournamentStepTableMemberID"></param>
        /// <returns></returns>
        public ResultMessage CheckRelationship(SYS_TournamentStepTableMember objTournamentStepTableMember)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                //using (PokerLife db = new PokerLife())
                //{
                //    SYS_TournamentStepTableMember user = db.SYS_TournamentStepTableMembers.FirstOrDefault(i => i.TournamentStepTableMemberGroupIDSys == objTournamentStepTableMemberGroup.IDSys);

                //    if (user != null && user.UID.Trim().Length > 0)
                //    {
                //        objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.No_Error, "Có dữ liệu liên quan đến nhóm thành viên tham gia", string.Empty);
                //    }
                //}
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.CheckData, "Lỗi kiểm tra dữ liệu liên quan thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> CheckRelationship");
                return objResultMessage;
            }
            return objResultMessage;
        }

        /// <summary>
        /// Thêm mới thành viên tham gia không cần kiểm tra
        /// </summary>
        /// <param name="objTournamentStepTableMember"></param>
        /// <returns></returns>
        public ResultMessage InsertNoCheck(SYS_TournamentStepTableMember objTournamentSTM)
        {
            ResultMessage objResultMessage = new ResultMessage();
            SYS_TournamentStepTableMember TournamentStepTableMember = new SYS_TournamentStepTableMember();

            try
            {
                Library.DataAccess.IData objIData = Library.DataAccess.Data.CreateData(DAL_Globals.ConnectString);
                objIData.ConnectNoEncrypt();
                SqlParameter[] para = new SqlParameter[7];
                para[0] = new SqlParameter("@TournamentIDSys", objTournamentSTM.TournamentIDSys);
                para[1] = new SqlParameter("@TournamentStepIDSys", objTournamentSTM.TournamentStepIDSys);
                para[2] = new SqlParameter("@TournamentStepTableIDSys", objTournamentSTM.TournamentStepTableIDSys);
                para[3] = new SqlParameter("@MemberIDSys", objTournamentSTM.MemberIDSys);
                para[4] = new SqlParameter("@IsLeader", objTournamentSTM.IsLeader);
                para[5] = new SqlParameter("@PrizeIDSys", objTournamentSTM.PrizeIDSys);
                para[6] = new SqlParameter("@Note", objTournamentSTM.Note);

                objIData.CreateStoredProcedue("sp_SYS_TournamentStepTableMember_ADD");
                objIData.AddParameter(para);
                objIData.ExecProcedure();

                DAL_Event.Insert("Thêm mới thành viên tham gia - " + TournamentStepTableMember.IDSys.ToString(), 0, ResultMessage.ErrorTypes.Insert.ToString());
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Insert, "Lỗi thêm thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> Insert");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Thêm thành viên tham gia thành công", string.Empty);
        }

        public ResultMessage InsertNoCheck(Library.DataAccess.IData objIData,Int64 intTournamentSTIDSys, List<SYS_TournamentStepTableMember> lstTournamentSTM)
        {
            ResultMessage objResultMessage = new ResultMessage();
            SYS_TournamentStepTableMember TournamentStepTableMember = new SYS_TournamentStepTableMember();

            try
            {
                foreach (SYS_TournamentStepTableMember objTournamentSTM in lstTournamentSTM)
                {
                    SqlParameter[] para = new SqlParameter[7];
                    para[0] = new SqlParameter("@TournamentIDSys", objTournamentSTM.TournamentIDSys);
                    para[1] = new SqlParameter("@TournamentStepIDSys", objTournamentSTM.TournamentStepIDSys);
                    para[2] = new SqlParameter("@TournamentStepTableIDSys", intTournamentSTIDSys);
                    para[3] = new SqlParameter("@MemberIDSys", objTournamentSTM.MemberIDSys);
                    para[4] = new SqlParameter("@IsLeader", objTournamentSTM.IsLeader);
                    para[5] = new SqlParameter("@PrizeIDSys", objTournamentSTM.PrizeIDSys);
                    para[6] = new SqlParameter("@Note", objTournamentSTM.Note);

                    objIData.CreateStoredProcedue("sp_SYS_TournamentStepTableMember_ADD");
                    objIData.AddParameter(para);
                    objIData.ExecProcedure();
                }

                DAL_Event.Insert("Thêm mới thành viên tham gia - " + TournamentStepTableMember.IDSys.ToString(), 0, ResultMessage.ErrorTypes.Insert.ToString());
            }
            catch (Exception objEx)
            {
                objIData.RollBackTransaction();
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Insert, "Lỗi thêm thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> Insert");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Thêm thành viên tham gia thành công", string.Empty);
        }

        /// <summary>
        /// Cập nhật thành viên tham gia không cần kiểm tra
        /// </summary>
        /// <param name="objTournamentStepTableMember"></param>
        /// <returns></returns>
        public ResultMessage UpdateNoCheck(SYS_TournamentStepTableMember objTournamentSTM)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                Library.DataAccess.IData objIData = Library.DataAccess.Data.CreateData(DAL_Globals.ConnectString);
                objIData.ConnectNoEncrypt();
                SqlParameter[] para = new SqlParameter[8];
                para[0] = new SqlParameter("@IDSys", objTournamentSTM.IDSys);
                para[1] = new SqlParameter("@TournamentIDSys", objTournamentSTM.TournamentIDSys);
                para[2] = new SqlParameter("@TournamentStepIDSys", objTournamentSTM.TournamentStepIDSys);
                para[3] = new SqlParameter("@TournamentStepTableIDSys", objTournamentSTM.TournamentStepTableIDSys);
                para[4] = new SqlParameter("@MemberIDSys", objTournamentSTM.MemberIDSys);
                para[5] = new SqlParameter("@IsLeader", objTournamentSTM.IsLeader);
                para[6] = new SqlParameter("@PrizeIDSys", objTournamentSTM.PrizeIDSys);
                para[7] = new SqlParameter("@Note", objTournamentSTM.Note);

                objIData.CreateStoredProcedue("sp_SYS_TournamentStepTableMember_UPD");
                objIData.AddParameter(para);
                objIData.ExecProcedure();

                DAL_Event.Insert("Cập nhật thành viên tham gia - " + objTournamentSTM.IDSys.ToString(), 0, ResultMessage.ErrorTypes.Update.ToString());
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Update, "Lỗi cập nhật thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> Update");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Cập nhật thành viên tham gia thành công", string.Empty);
        }

        /// <summary>
        /// Xóa thành viên tham gia không cần kiểm tra
        /// </summary>
        /// <param name="objTournamentStepTableMember"></param>
        /// <returns></returns>
        public ResultMessage DeleteNoCheck(SYS_TournamentStepTableMember objTournamentSTM)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                Library.DataAccess.IData objIData = Library.DataAccess.Data.CreateData(DAL_Globals.ConnectString);
                objIData.ConnectNoEncrypt();
                SqlParameter[] para = new SqlParameter[8];
                para[0] = new SqlParameter("@IDSys", objTournamentSTM.IDSys);

                objIData.CreateStoredProcedue("sp_SYS_TournamentStepTableMember_DEL");
                objIData.AddParameter(para);
                objIData.ExecProcedure();

                DAL_Event.Insert("Xóa thành viên tham gia - " + objTournamentSTM.IDSys.ToString(), 0, ResultMessage.ErrorTypes.Delete.ToString());

            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Delete, "Lỗi xóa thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> DeleteNoCheck");
                return objResultMessage;
            }
            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Xóa thành viên tham gia thành công", string.Empty);
        }               

        #endregion

        #region Load/Search/Select

        /// <summary>
        /// Lấy thông tin thành viên tham gia
        /// </summary>
        /// <param name="objTournamentStepTableMember"></param>
        /// <param name="strID"></param>
        /// <returns></returns>
        public ResultMessage LoadInfo(ref SYS_TournamentStepTableMember objTournamentStepTableMember, int intTournamentStepTableMemberIDSys)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    var TournamentStepTableMember = from gu in db.SYS_TournamentStepTableMembers
                               where gu.IDSys == intTournamentStepTableMemberIDSys
                               select gu;
                    objTournamentStepTableMember = new SYS_TournamentStepTableMember();
                    foreach (var obj in TournamentStepTableMember)
                    {
                        if (!Convert.IsDBNull(obj.IDSys)) objTournamentStepTableMember.IDSys = obj.IDSys;
                        if (!Convert.IsDBNull(obj.TournamentIDSys)) objTournamentStepTableMember.TournamentIDSys = obj.TournamentIDSys;
                        if (!Convert.IsDBNull(obj.TournamentStepIDSys)) objTournamentStepTableMember.TournamentStepIDSys = obj.TournamentStepIDSys;
                        if (!Convert.IsDBNull(obj.TournamentStepTableIDSys)) objTournamentStepTableMember.TournamentStepTableIDSys = obj.TournamentStepTableIDSys;
                        if (!Convert.IsDBNull(obj.MemberIDSys)) objTournamentStepTableMember.MemberIDSys = obj.MemberIDSys;
                        if (!Convert.IsDBNull(obj.IsLeader)) objTournamentStepTableMember.IsLeader = obj.IsLeader;
                        if (!Convert.IsDBNull(obj.PrizeIDSys)) objTournamentStepTableMember.PrizeIDSys = obj.PrizeIDSys;
                        if (!string.IsNullOrEmpty(obj.Note)) objTournamentStepTableMember.Note = obj.Note;                        
                        if (!Convert.IsDBNull(obj.OrderIndex)) objTournamentStepTableMember.OrderIndex = obj.OrderIndex;
                        if (!Convert.IsDBNull(obj.IsDeleted)) objTournamentStepTableMember.IsDeleted = obj.IsDeleted;
                        if (!Convert.IsDBNull(obj.DeletedDate)) objTournamentStepTableMember.DeletedDate = obj.DeletedDate;
                    }
                }

            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.LoadInfo, "Lấy thông tin thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> LoadInfo");
                return objResultMessage;
            }
            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy thông tin thành viên tham gia thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách thành viên tham gia
        /// </summary>
        /// <param name="dtTournamentStepTableMember"></param>
        /// <returns></returns>
        public ResultMessage SearchData(ref DataTable dtTournamentStepTableMember)
        {
            ResultMessage objResultMessage = new ResultMessage();
            dtTournamentStepTableMember = new DataTable();

            try
            {
                using (PokerLife db = new PokerLife())
                {
                    dtTournamentStepTableMember = ConvertData.ConvertToDataTable<SYS_TournamentStepTableMember>(db.SYS_TournamentStepTableMembers.Where(i => i.IsDeleted == false).ToList());
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SearchData, "Lấy thông tin thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> LoadInfo");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách thành viên tham gia thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách thành viên tham gia dạng List
        /// </summary>
        /// <param name="lstTournamentStepTableMember"></param>
        /// <returns></returns>
        public ResultMessage SelectToList(ref List<SYS_TournamentStepTableMember> lstTournamentStepTableMember)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    var TournamentStepTableMember = from gu in db.SYS_TournamentStepTableMembers
                               where gu.IsDeleted == false
                               orderby gu.OrderIndex
                               select gu;
                    lstTournamentStepTableMember = TournamentStepTableMember.ToList();
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SelectData, "Lấy thông tin thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> SelectToList");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách thành viên tham gia thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách thành viên tham gia dạng List
        /// </summary>
        /// <param name="lstTournamentStepTableMember">Đối tượng thành viên tham gia</param>
        /// <param name="intPageSize">Số lượng Record một trang</param>
        /// <param name="intPageNumber">Lấy trang số mấy</param>
        /// <returns></returns>
        public ResultMessage SelectToList(ref List<SYS_TournamentStepTableMember> lstTournamentStepTableMember, int intPageSize, int intPageNumber)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    var TournamentStepTableMember = from gu in db.SYS_TournamentStepTableMembers
                               where gu.IsDeleted == false
                               orderby gu.OrderIndex
                               select gu;

                    lstTournamentStepTableMember = TournamentStepTableMember.Skip((intPageNumber - 1) * intPageSize).Take(intPageSize).ToList();
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SelectData, "Lấy thông tin thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> SelectToList");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách thành viên tham gia thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách thành viên tham gia để Cache
        /// </summary>
        /// <param name="lstTournamentStepTableMember"></param>
        /// <returns></returns>
        public ResultMessage SelectToCache(ref List<SYS_TournamentStepTableMember> lstTournamentSTM, int intTournamentIDSys, Int64 intTournamentStepIDSys, Int64 intTournamentSTIDSys)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                Library.DataAccess.IData objIData = Library.DataAccess.Data.CreateData(DAL_Globals.ConnectString);
                objIData.ConnectNoEncrypt();
                SqlParameter[] para = new SqlParameter[3];
                para[0] = new SqlParameter("@TournamentIDSys", intTournamentIDSys);
                para[1] = new SqlParameter("@TournamentStepIDSys", intTournamentStepIDSys);
                para[2] = new SqlParameter("@TournamentStepTableIDSys", intTournamentSTIDSys);

                objIData.CreateStoredProcedue("sp_SYS_TournamentStepTableMember_SRH");
                objIData.AddParameter(para);
                objIData.ExecProcedure();
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SelectData, "Lấy Cache danh sách thành viên tham gia", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTableMember --> SelectToListCache");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy Cache danh sách thành viên tham gia thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách thành viên tham gia từ Cache
        /// </summary>
        /// <param name="lstTournamentStepTableMember"></param>
        /// <returns></returns>
        public ResultMessage SelectToListCache(ref List<SYS_TournamentStepTableMember> lstTournamentStepTableMember)
        {
            ResultMessage objResultMessage = new ResultMessage();
            //objResultMessage = MPR.Poker.DALEntity.DataCache.GetDataCache.GetTournamentStepTableMember(ref lstTournamentStepTableMember);
            return objResultMessage;
        }

        #endregion
    }
}
